package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.TestOptionApi;
import com.tanhua.dubbo.api.mapper.TestOptionMapper;
import com.tanhua.model.db.Option;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class TestOptionApiImpl implements TestOptionApi {

    @Autowired
    private TestOptionMapper testOptionMapper;

    @Override
    public List<Option> findByQuestionId(Integer questionId) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("question_id", questionId);
        return testOptionMapper.selectList(queryWrapper);
    }

    @Override
    public Integer findByIds(List<Long> ids) {
        QueryWrapper<Option> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<Option> options = testOptionMapper.selectList(queryWrapper);
        Integer total = 0;
        List<Integer> collect = options.stream().map(Option::getScore).collect(Collectors.toList());
        for (Integer score : collect) {
            total += score;
        }
        return total;
    }
}
